export class BallColor {
  // 0 默认颜色 1 全色（单色） 2 双色 3 黑8 4 红色 5 彩色
  public static UnknowColorBall = 0;
  public static SingleColorBall = 1;
  public static TwoColorBall = 2;
  public static BlackBallEight = 3;
  public static RedColorBall = 4;
  public static ColoredColorBall = 5;
  public static AnyColorBall = 6;
  public static SpecialColorBall = 100; // 特殊花色
  public color = 0;
  public static oppoColor(clr: number) {
    return clr === 1 ? 2 : 1;
  }
  constructor(clr: number) { this.color = clr;}
  isUnknowColor() {
    return this.color === BallColor.UnknowColorBall;
  }
  isSureColored() {
    return this.color !== BallColor.UnknowColorBall;
  }
  initColor() {
    this.color = BallColor.UnknowColorBall;
  }
  isSameColor(clr: number) {
    return this.color === clr;
  }
  initSingleColor() {
    this.color = BallColor.SingleColorBall;
  }
  initTwoColor() {
    this.color = BallColor.TwoColorBall;
  }
  initRedColor() {
    this.color = BallColor.RedColorBall;
  }
  initColoredColor() {
    this.color = BallColor.ColoredColorBall;
  }
  initAnyColor() {
    this.color = BallColor.AnyColorBall;
  }
  initSpecialColor() {
    this.color = BallColor.SpecialColorBall;
  }
  swapColor() {
    if (this.color === BallColor.RedColorBall) {
      this.color = BallColor.ColoredColorBall;
    } else if (this.color === BallColor.ColoredColorBall) {
      this.color = BallColor.RedColorBall;
    }
  }
  isRedColorBall() {
    return this.color === BallColor.RedColorBall;
  }
  isColoredColorBall() {
    return this.color === BallColor.ColoredColorBall;
  }
  isSpecialColor() {
    return this.color === BallColor.SpecialColorBall;
  }
  isBlackBallEight() {
    return this.color === BallColor.BlackBallEight;
  }
  // 球颜色的文本
  public getColorText() {
    switch (this.color) {
      case BallColor.SingleColorBall:
        return '单色球';
      case BallColor.TwoColorBall:
        return '双色球';
      case BallColor.RedColorBall:
        return '红色球';
      case BallColor.ColoredColorBall:
        return '彩色球';
      case BallColor.BlackBallEight:
        return '黑8球';
      case BallColor.SpecialColorBall:
        return '最小号';
      case BallColor.AnyColorBall:
        return '任意球';
      default :
        return '未确定颜色';
    }
  }
}
/**
 * 球的属性
 */
export class BallAttribute {
  // 1 全色（单色） 2 双色 3 黑8 4 红色 5 彩色
  public static SingleColorBall = 1;
  public static TwoColorBall = 2;
  public static BlackColorBall = 3;
  public static RedColorBall = 4;
  public static ColoredColorBall = 5;
  // 白球的标号 100
  public static MainBallIndex = 100;
  public static radius = 0.50;
  public static diameter = 1.00;
  public static sqrtThree = 1.734;
  public static gap = 0.005;
  public static eightBallPoints = new Map([
    ['BallSphere1', { x: -10, y: 0, z: 0 }],
    ['BallSphere2', { x: -10 -BallAttribute.sqrtThree / 2 - BallAttribute.gap, y: 0, z: -0.5 - BallAttribute.gap }],
    ['BallSphere3', { x: -10 -BallAttribute.sqrtThree / 2 - BallAttribute.gap, y: 0, z: 0.5 + BallAttribute.gap}],
    ['BallSphere4', { x: -10 -BallAttribute.sqrtThree - 2 * BallAttribute.gap, y: 0, z: -1 - BallAttribute.gap}],
    ['BallSphere5', { x: -10 -BallAttribute.sqrtThree - 2 * BallAttribute.gap, y: 0, z: 0 }],
    ['BallSphere6', { x: -10 -BallAttribute.sqrtThree - 2 * BallAttribute.gap, y: 0, z: 1 + BallAttribute.gap}],
    ['BallSphere7', { x: -10 -BallAttribute.sqrtThree * 1.5 - 3 * BallAttribute.gap, y: 0, z: -1.5 - 2 * BallAttribute.gap}],
    ['BallSphere8', { x: -10 -BallAttribute.sqrtThree * 1.5 - 3 * BallAttribute.gap, y: 0, z: -0.5 - BallAttribute.gap }],
    ['BallSphere9', { x: -10 -BallAttribute.sqrtThree * 1.5 - 3 * BallAttribute.gap, y: 0, z: 0.5 + BallAttribute.gap}],
    ['BallSphere10', { x: -10 -BallAttribute.sqrtThree * 1.5 - 3 * BallAttribute.gap, y: 0, z: 1.5 + 2 * BallAttribute.gap}],
    ['BallSphere11', { x: -10 -BallAttribute.sqrtThree * 2 - 4 * BallAttribute.gap, y: 0, z: -2 - 2 * BallAttribute.gap}],
    ['BallSphere12', { x: -10 -BallAttribute.sqrtThree * 2 - 4 * BallAttribute.gap, y: 0, z: -1 - BallAttribute.gap}],
    ['BallSphere13', { x: -10 -BallAttribute.sqrtThree * 2 - 4 * BallAttribute.gap, y: 0, z: 0 }],
    ['BallSphere14', { x: -10 -BallAttribute.sqrtThree * 2 - 4 * BallAttribute.gap, y: 0, z: 1 - BallAttribute.gap}],
    ['BallSphere15', { x: -10 -BallAttribute.sqrtThree * 2 - 4 * BallAttribute.gap, y: 0, z: 2 - 2 * BallAttribute.gap}],
  ]);
  public static snookerBallNamePoints = new Map([
    ['BallSphereRed1', { x: -9, y: 0, z: 0 }],
    ['BallSphereRed2', { x: -9 - BallAttribute.sqrtThree / 2, y: 0, z: -0.5 }],
    ['BallSphereRed3', { x: -9 -BallAttribute.sqrtThree / 2, y: 0, z: 0.5 }],
    ['BallSphereRed4', { x: -9 -BallAttribute.sqrtThree, y: 0, z: -1 }],
    ['BallSphereRed5', { x: -9 -BallAttribute.sqrtThree, y: 0, z: 0 }],
    ['BallSphereRed6', { x: -9 -BallAttribute.sqrtThree, y: 0, z: 1 }],
    ['BallSphereRed7', { x: -9 -BallAttribute.sqrtThree * 1.5, y: 0, z: -1.5 }],
    ['BallSphereRed8', { x: -9 -BallAttribute.sqrtThree * 1.5, y: 0, z: -0.5 }],
    ['BallSphereRed9', { x: -9 -BallAttribute.sqrtThree * 1.5, y: 0, z: 0.5 }],
    ['BallSphereRed10', { x: -9 -BallAttribute.sqrtThree * 1.5, y: 0, z: 1.5 }],
    ['BallSphereRed11',{ x: -9 -BallAttribute.sqrtThree * 2, y: 0, z: -2 }],
    ['BallSphereRed12', { x: -9 -BallAttribute.sqrtThree * 2, y: 0, z: -1 }],
    ['BallSphereRed13', { x: -9 -BallAttribute.sqrtThree * 2, y: 0, z: 0 }],
    ['BallSphereRed14', { x: -9 -BallAttribute.sqrtThree * 2, y: 0, z: 1 }],
    ['BallSphereRed15', { x: -9 -BallAttribute.sqrtThree * 2, y: 0, z: 2 }],
    ['BallSphereYellow', { x: 10, y: 0, z: -5 }],
    ['BallSphereGreen', { x: 10, y: 0, z: 5 }],
    ['BallSphereCoffee', { x: 10, y: 0, z: 0 }],
    ['BallSphereBlue', { x: 0, y: 0, z: 0 }],
    ['BallSpherePink', { x: -7.5, y: 0, z: 0 }],
    ['BallSphereBlack', { x: -15, y: 0, z: 0 }],
  ]);

  public static nineBallNamePoints = new Map([
    ['BallSphere1', { x: -10, y: 0, z: 0 }],
    ['BallSphere2', { x: -10 - BallAttribute.sqrtThree / 2, y: 0, z: -0.5 }],
    ['BallSphere3', { x: -10 -BallAttribute.sqrtThree / 2, y: 0, z: 0.5 }],
    ['BallSphere4', { x: -10 -BallAttribute.sqrtThree, y: 0, z: -1 }],
    ['BallSphere5', { x: -10 -BallAttribute.sqrtThree * 2, y: 0, z: 0 }],
    ['BallSphere6', { x: -10 -BallAttribute.sqrtThree, y: 0, z: 1 }],
    ['BallSphere7', { x: -10 -BallAttribute.sqrtThree * 1.5, y: 0, z: -0.5 }],
    ['BallSphere8', { x: -10 -BallAttribute.sqrtThree * 1.5, y: 0, z: 0.5 }],
    ['BallSphere9', { x: -10 -BallAttribute.sqrtThree, y: 0, z: 0 }],
  ]);

  public static nameToBalls = new Map([
    ['BallSphereMain', new BallAttribute(100, 0, 0)],
    ['BallSphere1', new BallAttribute(1, 1, 1)],
    ['BallSphere2', new BallAttribute(2, 1, 1)],
    ['BallSphere3', new BallAttribute(3, 1, 1)],
    ['BallSphere4', new BallAttribute(4, 1, 1)],
    ['BallSphere5', new BallAttribute(5, 1, 1)],
    ['BallSphere6', new BallAttribute(6, 1, 1)],
    ['BallSphere7', new BallAttribute(7, 1, 1)],
    ['BallSphere8', new BallAttribute(8, 3, 100)],
    ['BallSphere9', new BallAttribute(9, 2, 1)],
    ['BallSphere10', new BallAttribute(10, 2, 1)],
    ['BallSphere11', new BallAttribute(11, 2, 1)],
    ['BallSphere12', new BallAttribute(12, 2, 1)],
    ['BallSphere13', new BallAttribute(13, 2, 1)],
    ['BallSphere14', new BallAttribute(14, 2, 1)],
    ['BallSphere15', new BallAttribute(15, 2, 1)],

    ['BallSphereRed1', new BallAttribute(1, 4, 1)],
    ['BallSphereRed2', new BallAttribute(2, 4, 1)],
    ['BallSphereRed3', new BallAttribute(3, 4, 1)],
    ['BallSphereRed4', new BallAttribute(4, 4, 1)],
    ['BallSphereRed5', new BallAttribute(5, 4, 1)],
    ['BallSphereRed6', new BallAttribute(6, 4, 1)],
    ['BallSphereRed7', new BallAttribute(7, 4, 1)],
    ['BallSphereRed8', new BallAttribute(8, 4, 1)],
    ['BallSphereRed9', new BallAttribute(9, 4, 1)],
    ['BallSphereRed10', new BallAttribute(10, 4, 1)],
    ['BallSphereRed11', new BallAttribute(11, 4, 1)],
    ['BallSphereRed12', new BallAttribute(12, 4, 1)],
    ['BallSphereRed13', new BallAttribute(13, 4, 1)],
    ['BallSphereRed14', new BallAttribute(14, 4, 1)],
    ['BallSphereRed15', new BallAttribute(15, 4, 1)],

    ['BallSphereYellow', new BallAttribute(16, 5, 2)],
    ['BallSphereGreen', new BallAttribute(17, 5, 3)],
    ['BallSphereCoffee', new BallAttribute(18, 5, 4)],
    ['BallSphereBlue', new BallAttribute(19, 5, 5)],
    ['BallSpherePink', new BallAttribute(20, 5, 6)],
    ['BallSphereBlack', new BallAttribute(21, 5, 7)],
  ]);
  public static nameToChinessName = new Map([
    ['BallSphereMain', '白球'],
    ['BallSphere1', '1号球黄球'],
    ['BallSphere2', '2号球蓝球'],
    ['BallSphere3', '3号球红球'],
    ['BallSphere4', '4号球紫球'],
    ['BallSphere5', '5号球橙球'],
    ['BallSphere6', '6号球绿球'],
    ['BallSphere7', '7号球棕球'],
    ['BallSphere8', '8号黑球'],
    ['BallSphere9', '9号球黄球'],
    ['BallSphere10', '10号球蓝球'],
    ['BallSphere11', '11号球红球'],
    ['BallSphere12', '12号紫球'],
    ['BallSphere13', '13号球橙球'],
    ['BallSphere14', '14号球绿球'],
    ['BallSphere15', '15号球棕球'],

    ['BallSphereRed1', '红球'],
    ['BallSphereRed2', '红球'],
    ['BallSphereRed3', '红球'],
    ['BallSphereRed4', '红球'],
    ['BallSphereRed5', '红球'],
    ['BallSphereRed6', '红球'],
    ['BallSphereRed7', '红球'],
    ['BallSphereRed8', '红球'],
    ['BallSphereRed9', '红球'],
    ['BallSphereRed10', '红球'],
    ['BallSphereRed11', '红球'],
    ['BallSphereRed12', '红球'],
    ['BallSphereRed13', '红球'],
    ['BallSphereRed14', '红球'],
    ['BallSphereRed15', '红球'],

    ['BallSphereYellow', '黄球'],
    ['BallSphereGreen', '绿球'],
    ['BallSphereCoffee', '咖啡球'],
    ['BallSphereBlue', '蓝球'],
    ['BallSpherePink', '粉球'],
    ['BallSphereBlack', '黑球'], 
  ]);
  public static getBallByName(name: string) : BallAttribute {
    if (BallAttribute.nameToBalls.has(name)) {
      return BallAttribute.nameToBalls.get(name);
    }
    return null;
  }
  public static getBallRandomIndexs() {
    const indexs = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15];
    for (var i=0; i<15; i++) {
      const a = Math.floor(Math.random() * 15);
      [indexs[a], indexs[i]] = [indexs[i], indexs[a]];
    }
    // 黑8在第5号位置
    for (var i=0; i<15; i++) {
      if (indexs[i] === 5) {
        [indexs[7], indexs[i]] = [indexs[i], indexs[7]];
        break;
      }
    }
    return indexs;
  }
  public static getNinePointByName(name: string) {
    if (BallAttribute.nineBallNamePoints.has(name)) {
      return BallAttribute.nineBallNamePoints.get(name);
    }
    return {x: 0, y: 0, z: 0};
  }
  public static getSnookerPointByName(name: string) {
    if (BallAttribute.snookerBallNamePoints.has(name)) {
      return BallAttribute.snookerBallNamePoints.get(name);
    }
    return {x: 0, y: 0, z: 0};
  }
  public static getChineseName(name: string) {
    return BallAttribute.nameToChinessName.get(name);
  }
  public static snookerBlackBallName() {
    return "BallSphereBlack";
  }
  // 基础属性
  public index = 0; // 标号
  public color = 0; 
  public score = 1;
  constructor(idx: number, clr: number, sc: number) {
    this.index = idx;
    this.color = clr;
    this.score = sc;
  }
  public isSingleColorBall() {
    return this.color === BallColor.SingleColorBall;
  }
  public isTwoColorBall() {
    return this.color === BallColor.TwoColorBall;
  }
  public isRedColorBall() {
    return this.color === BallColor.RedColorBall;
  }
  public isColoredColorBall() {
    return this.color === BallColor.ColoredColorBall;
  }
  public isBlackBall8() {
    return this.index === 8 && this.color === BallAttribute.BlackColorBall;
  }
  public isNineBall9() {
    return this.index === 9 && this.color === BallAttribute.TwoColorBall;
  }
  public isMainWhiteBall() {
    return this.index === 100;
  }
  public isSameBall(ba: BallAttribute) {
    return this.index === ba.index && this.color === ba.color;
  }
  public ballName() {
    for (const [name, ba] of BallAttribute.nameToBalls) {
      if (ba.index === this.index && ba.color === this.color) {
        return name;
      }
    }
    return '';
  }
}